package com.eorchis.module.department.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.eorchis.constants.DeptConstants;
import com.eorchis.core.basedao.base.IBaseDao;
import com.eorchis.core.servicetemplate.treetemplate.jsonbean.jsonnodeparameters.UpdateOrderParameter;
import com.eorchis.module.department.dao.IDepartmentTreeDao;
import com.eorchis.module.department.dao.require.DepartmentTreeRequire;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.department.domain.DepartmentTreeCondition;
import com.eorchis.module.department.domain.jsonbean.DepartmentTreeJsonBean;
import com.eorchis.utils.utils.PropertyUtil;

@SuppressWarnings("deprecation")
@Repository("com.eorchis.module.department.dao.impl.DepartmentTreeDaoImpl")
public class DepartmentTreeDaoImpl implements IDepartmentTreeDao {

	@Autowired
	@Qualifier("com.eorchis.module.department.dao.require.DepartmentTreeRequire")
	private DepartmentTreeRequire departmentTreeSyncRequire;

	@Autowired
	@Qualifier("com.eorchis.core.basedao.base.impl.BaseDao")
	private IBaseDao<Department> baseDao;

	public boolean isLeapNode(DepartmentTreeCondition condition) throws Exception {
		boolean result = true;
		this.departmentTreeSyncRequire.isLeapNode(condition);
		long countQuery = this.baseDao.countQuery(condition);
		if (countQuery > 0L) {
			result = false;
		}
		return result;
	}

	public void updateTreeOrderNum(UpdateOrderParameter updateOrderParameter) throws Exception {
		DepartmentTreeCondition condition = this.departmentTreeSyncRequire.updateTreeOrderNumHQL(updateOrderParameter);
		this.baseDao.executeUpdate(condition);
	}

	public void updateTreeParentID(DepartmentTreeCondition condition) throws Exception {
		this.departmentTreeSyncRequire.updateTreeParentIDHQL(condition);
		this.baseDao.executeUpdate(condition);
	}

	public List<Department> listDepartmentTree(DepartmentTreeCondition condition) throws Exception {
		if (PropertyUtil.objectNotEmpty(condition.getSearchType()) && condition.getSearchType().equals(DeptConstants.DEPT_LEVEL_CODE_LV1))
			this.departmentTreeSyncRequire.listDepartmentTreeHQLForLv1(condition);
		else
			this.departmentTreeSyncRequire.listDepartmentTreeHQL(condition);
		List list = this.baseDao.find(condition);
		return list;
	}
	
	public List<Department> listUnSeclectDepartmentTree(DepartmentTreeCondition condition) throws Exception {
		return this.departmentTreeSyncRequire.listUnSeclectDepartmentTreeHQL(condition);
	}

	public List<DepartmentTreeJsonBean> listDepartmentCheckedTree(DepartmentTreeCondition condition) throws Exception {
		this.departmentTreeSyncRequire.listDepartmentCheckedTreeHQL(condition);
		condition.setBasebean(DepartmentTreeJsonBean.class);
		Query query = createQuery(condition.getSql(), condition.getParameterList());
		if (condition.getBasebean() != null) {
			query.setResultTransformer(Transformers.aliasToBean(condition.getBasebean()));
		}
		List list = query.list();
		return list;
	}

	public Query createQuery(String hql, List<Object> parameterList) throws Exception, Exception {
		Assert.hasText(hql);
		Query query = this.baseDao.getHibernateSession().createQuery(hql);
		if (!parameterList.isEmpty()) {
			for (int i = 0; i < parameterList.size(); i++) {
				query.setParameter(i, parameterList.get(i));
			}
		}
		return query;
	}

	public Department getDepartment(DepartmentTreeCondition condition) throws Exception {
		return (Department) this.baseDao.loadObject(Department.class, condition.getSearchDepID());
	}

	public void addDepartmentTree(Department department) throws Exception {
		this.baseDao.addEntity(department);
		this.baseDao.getHibernateSession().flush();
	}

	public void updateDepartmentTree(Department department) throws Exception {
		this.baseDao.updateEntityByPK(department);
	}

	public List<Department> listDepartmentProjectTree(DepartmentTreeCondition condition) throws Exception {
		this.departmentTreeSyncRequire.listDepartmentProjectTreeHQL(condition);
		List list = this.baseDao.find(condition);
		return list;
	}
}